package com.core.db.execute;

import java.sql.SQLException;
import java.util.List;

import com.core.db.condition.Condition;
import com.core.db.require.Assembler;
import com.core.db.require.Require;

/**
 * sql事务
 * 
 * @author 殷田
 *
 */
public class Transaction
{
	private Executor executor;
	private Worker worker;
	private boolean isError = false;

	public Transaction( Worker worker )
	{
		this.worker = worker;
	}

	/**
	 * 查询操作
	 * 
	 * @param clazz
	 * @param cond
	 * @return
	 * @throws SQLException
	 */
	public < T > List< T > query( Class<T> clazz ,Condition cond) throws SQLException
	{
		try
		{
			Require require = Assembler.query( clazz, cond );
			Result result = new Executor().query( require );
			List< T > values = Assembler.parseResult( result.getResultSet(),require.getColumnDescribes(), clazz );
			result.release();
			return values;
		}
		catch ( SQLException e )
		{
			isError = true;
			throw e;
		}
		
	}

	/**
	 * 插入操作
	 * 
	 * @param obj
	 * @return
	 * @throws SQLException
	 */
	public boolean insert( Object obj ) throws SQLException
	{
		try
		{
			Require require=Assembler.insert( obj);
			return getExecutor().insert( require );
		}
		catch ( SQLException e )
		{
			isError = true;
			throw e;
		}
	}

	/**
	 * 删除操作
	 * 
	 * @param clazz
	 * @param cond
	 * @return
	 * @throws SQLException
	 */
	public boolean delect( Class<?> clazz ,Condition cond ) throws SQLException
	{
		try
		{
			Require require=Assembler.delect( clazz, cond );
			return getExecutor().delect( require );
		}
		catch ( SQLException e )
		{
			isError = true;
			throw e;
		}
	}

	/**
	 * 更新操作
	 * 
	 * @param obj
	 * @param cond
	 * @return
	 * @throws SQLException
	 */
	public boolean update( Object obj ,Condition cond ) throws SQLException
	{
		try
		{
			Require require=Assembler.update( obj, cond );
			return getExecutor().update( require );
		}
		catch ( SQLException e )
		{
			isError = true;
			throw e;
		}
	}
	
	/**
	 * 直接执行查询sql
	 * 
	 * @param table
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public Result rawQuery( String sql ) throws SQLException
	{
		try
		{
			return getExecutor().rawQuery( sql );
		}
		catch ( SQLException e )
		{
			isError = true;
			throw e;
		}
	}

	/**
	 * 直接执行sql语句
	 * 
	 * @param table
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public boolean raw( String sql ) throws SQLException
	{
		try
		{
			return getExecutor().raw( sql );
		}
		catch ( SQLException e )
		{
			isError = true;
			throw e;
		}
	}

	private Executor getExecutor()
	{
		if ( executor == null )
		{
			executor = new Executor( false );
		}
		return executor;
	}

	/**
	 * 执行事务
	 */
	public void exec()
	{
		try
		{
			isError = false;
			if ( worker != null )
			{
				worker.exec( this );
			}
			if ( isError )
			{
				executor.getConnection().rollback();
			}
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
	}

	public interface Worker
	{
		public void exec( Transaction trans );
	}
}
